/*
 * Copyright (c) sl All rights reserved.
 * 本软件源代码版权归sl所有,未经许可不得任意复制与传播.
 */
package com.sl.crawler.manager;

import com.sl.crawler.common.enums.EmYn;
import com.sl.crawler.common.utils.DateUtils;
import com.sl.crawler.dao.HouseDetailDayDao;
import com.sl.crawler.dao.base.BaseDao;
import com.sl.crawler.domain.House;
import com.sl.crawler.domain.HouseDetailDay;
import com.sl.crawler.manager.base.BaseManager;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author sl
 */
@Service
public class HouseDetailDayManager extends BaseManager<HouseDetailDay, Long> {
    private static final Logger LOG = LoggerFactory.getLogger(HouseDetailDayManager.class);
    @Autowired
    private HouseDetailDayDao houseDetailDayDao;

    public BaseDao<HouseDetailDay, Long> getDao() {
        return houseDetailDayDao;
    }
    //自定义扩展

    public void saveOrUpdateHouseDetailDay(House house) {
        HouseDetailDay houseDetailDay = buildHouseDetailDay(house);

        HouseDetailDay houseDetailDayParam = new HouseDetailDay();
        houseDetailDayParam.setHouseId(house.getHouseId());
        houseDetailDayParam.setYn(EmYn.YES.value());
        houseDetailDayParam.setCrawlerDate(DateUtils.format(DateUtils.yyyy_MM_dd));
        HouseDetailDay houseDetailDayDb = this.selectOneEntry(houseDetailDayParam);
        if (houseDetailDayDb == null || houseDetailDayDb.getId() == null) {
            this.insertEntry(houseDetailDay);
        } else {
            house.setId(houseDetailDayDb.getId());
            this.updateByKey(houseDetailDay);
        }
    }

    private HouseDetailDay buildHouseDetailDay(House house) {
        HouseDetailDay houseDetailDay = new HouseDetailDay();
        try {
            BeanUtils.copyProperties(houseDetailDay, house);
        } catch (Exception e) {
            LOG.error("每日明细对象转换失败", e);
        }
        houseDetailDay.setCrawlerDate(DateUtils.format(DateUtils.yyyy_MM_dd));
        return houseDetailDay;
    }

}